package ftdpc.centre;

import ftdpc.annotation.EnableDispatchTask;
import ftdpc.application.IDispatchTaskController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 类描述: 任务调度Controller注册处理
 *
 * @author : 王雷
 * @date : 2020/3/21 0021 下午 6:34
 */
@Slf4j
public class DispatchTaskControllerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    /*private final RequestMappingHandlerMapping requestMappingHandlerMapping;

    public DispatchTaskControllerRegistrar(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        this.requestMappingHandlerMapping = requestMappingHandlerMapping;
    }*/

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        //这里获取通过@EnableDispatchTask配置的路径信息
        AnnotationAttributes mapperScanAttrs = AnnotationAttributes
                .fromMap(importingClassMetadata.getAnnotationAttributes(EnableDispatchTask.class.getName()));
        assert mapperScanAttrs != null;
        Class<? extends IDispatchTaskController> tcClass = mapperScanAttrs.getClass("taskController");
        log.info("当前加载的任务调度管理交互对象为：{}", tcClass);
        final GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(tcClass);

        beanDefinitionRegistry.registerBeanDefinition("iDispatchTaskController", genericBeanDefinition);
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {

    }
}
